/**
 * ====================================
 * 
 * Session使用
 * 
 * ====================================
 */
const Koa = require('koa');
const router = require('koa-router')();
const koaSession = require('koa-session');

const app = new Koa();


// 配置koa-session中间件
app.keys = ['some secret hurr'];  // cookie签名

const CONFIG = {
	key: 'koa:sess', // [string] cookie key (default if koa:sess)
	
	maxAge: 86400000, // [number || 'session']
					  // maxAge in ms (default is 1 days)
					  // 'session' will result in a cookie that expires when session/browser in closed
					  // Warning: If a session cookie is stolen, this cookie will never expire
	
	overwrite: true,  // [boolean] can overwrite or not (default true)
	
	httpOnly: true,  // [boolean] httpOnly or not (default true)
	
	signed: true,  // [boolean] signed or not (default true)
	
	rolling: false, // [boolean] Force a session identifier cookie to be set on every response.
					// The expiration is reset to the original maxAge,
					// resetting the expiration countdown. (default false).
					
	renew: false, // [boolean] renew session when session is nearly expired,
				  // so we can always keep user logged in. (default false)
}

app.use(koaSession(CONFIG, app));






router.get('/', async (ctx) => {
	
	ctx.session.userinfo = 'zhangsan';   // 配置session
	ctx.body = '首页';
});


router.get('/news', async (ctx) => {
	
	ctx.body = 'session: ' + ctx.session.userinfo;  // 获取session
});



app.use(router.routes());
app.use(router.allowedMethods());


app.listen(8088);
console.log('Server run http://localhost:8088/');

